#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
vector<string> citys;
map<string, vector<string>> mp;

int main() {
    ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
    int n; cin >> n;
    for (int i = 1; i <= n; i++) {
        string s, p; cin >> s >> p;
        if (!mp.count(p)) {
            citys.push_back(p);
        }
        mp[p].push_back(s);
    }
    for (const auto& city : citys) {
        cout << city << " " << mp[city].size() << '\n';
        for (const auto& i : mp[city]) {
            cout << i << '\n';
        }
    }
    return 0;
}